package com.csi.bumblebee.controller.backstage;

import com.csi.bumblebee.domain.user.Company;
import com.csi.bumblebee.domain.user.Resume;
import com.csi.bumblebee.domain.user.Student;
import com.csi.bumblebee.domain.util.MyResult;
import com.csi.bumblebee.domain.util.StateCode;
import com.csi.bumblebee.service.ResumeService;
import com.csi.bumblebee.service.StudentService;
import com.github.pagehelper.PageInfo;
import com.csi.bumblebee.util.PageHelp;
import com.github.pagehelper.PageInfo;
import org.apache.catalina.valves.StuckThreadDetectionValve;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.plaf.nimbus.State;
import java.util.List;

/**
 * @author 孙伟俊
 */
@RestController
@RequestMapping("/backstage/student")
public class BackStageStudentController {

    @Autowired
    private ResumeService resumeService;

    @Autowired
    private StudentService studentService;

    /**
     * 展示所有学生的简历
     */
    @GetMapping("/resume")
    public MyResult<PageInfo<Resume>> list(@RequestParam(value = "currentPage",required = false) Integer currentPage,
                                           @RequestParam(value = "perPageSize",required = false) Integer perPageSize){

        PageInfo<Resume> resumeList = resumeService.listResume(currentPage,perPageSize);

//        MyResult<PageInfo<Resume>> myResult = new MyResult<>();
        MyResult<PageInfo<Resume>> myResult = new MyResult<>();
        myResult.setData(resumeList);
        myResult.setCode(StateCode.FIND_SUCCESS);
        myResult.setMessage("查询成功");

        return myResult;
    }

    /**
     * 禁用某个简历
     */
    @PutMapping("/resume/{id}")
    public MyResult<Integer> bannerResume(@PathVariable String id){

        int result = resumeService.banResume(id);

        MyResult<Integer> myResult = new MyResult<>();

        if (result > 0) {
            myResult.setData(result);
            myResult.setMessage("更新成功");
            myResult.setCode(StateCode.UPDATE_SUCCESS);
        }else {
            myResult.setData(result);
            myResult.setMessage("更新失败");
            myResult.setCode(StateCode.UPDATE_FAILURE);
        }

        return myResult;
    }

    /**
     * 删除某个简历
     * @param id 简历主键
     * @return MyResult
     */
    @DeleteMapping("/resume/{id}")
    public MyResult<Integer> delete(@PathVariable String id){
        System.out.println(id);

        MyResult<Integer> myResult = new MyResult<>(StateCode.DELETE_SUCCESS, "删除成功");

        int result = resumeService.delete(id);
        if (result > 0) {
            return myResult;
        }else {
            myResult.setCode(StateCode.DELETE_FAILURE);
            myResult.setMessage("删除失败");
        }
        return myResult;
    }

    /**
     * 展示所有已启用的账号
     * @return MyResult<List<Student>>
     */
    @GetMapping("/account")
    public MyResult<List<Student>> listStudent(){

        MyResult<List<Student>> myResult = new MyResult<>(StateCode.FIND_SUCCESS, "查询成功");
        List<Student> studentList = studentService.listStudentAccount();

        if (studentList != null) {
            myResult.setData(studentList);
            return myResult;
        }else {
            myResult.setMessage("查询失败");
            myResult.setCode(StateCode.DELETE_FAILURE);
        }

        return myResult;
    }

    /**
     * 展示所有已封禁的账号
     * @return MyResult<List<Student>>
     */
    @GetMapping("/bAccount")
    public MyResult<List<Student>> listBanAccount(){
        return null;
    }

    /**
     * 删除某个账号
     * @param id 账号主键
     * @return yResult<Integer>
     */
    @DeleteMapping("/account/{id}")
    public MyResult<Integer> deleteAccount(@PathVariable String id){
        MyResult<Integer> myResult = new MyResult<>(StateCode.DELETE_SUCCESS, "删除成功");

        int result = studentService.delete(id);
        if (result > 0){
            return myResult;
        }else {
            myResult.setCode(StateCode.DELETE_FAILURE);
            myResult.setMessage("删除失败");
        }
        return myResult;
    }

    /**
     * 封禁某个账号
     */
    @PutMapping("/bAccount/{id}")
    public MyResult<Integer> banAccount(@PathVariable String id){

        MyResult<Integer> myResult = new MyResult<>(StateCode.UPDATE_SUCCESS, "封禁成功");

        int result = studentService.bStudentAccount(id);
        if (result > 0){
            return myResult;
        }else {
            myResult.setCode(StateCode.UPDATE_FAILURE);
            myResult.setMessage("封禁失败");
        }
        return myResult;
    }

    /**
     * 展示所有处于黑名单中的账号
     * @return MyResult<List<Student>>
     */
    @GetMapping("/blackList")
    public MyResult<List<Student>> blackList(){

        MyResult<List<Student>> myResult = new MyResult<>(StateCode.FIND_SUCCESS, "查询成功");
        List<Student> studentList = studentService.listStudentBlackAccount();
        if (studentList != null) {
            myResult.setData(studentList);
        }else {
            myResult.setCode(StateCode.FIND_FAILURE);
            myResult.setMessage("查询失败");
        }
        return myResult;
    }

    /**
     * 解封某个账号
     * @return MyResult<Integer>
     */
    @PutMapping("/rAccount/{id}")
    public MyResult<Integer> releaseAccount(@PathVariable String id){
        MyResult<Integer> myResult = new MyResult<>(StateCode.UPDATE_SUCCESS, "解封成功");

        int result = studentService.releaseAccount(id);
        if (result > 0){
            return myResult;
        }else {
            myResult.setCode(StateCode.UPDATE_FAILURE);
            myResult.setMessage("解封失败");
        }
        return myResult;
    }

    /**
     * 展示所有注销的账号
     * @return MyResult<PageInfo<Student>>
     */
    @PostMapping("/canceledAccount")
    public MyResult<PageInfo<Student>> listCanceledAccount(@RequestBody PageHelp pageHelp){
        MyResult<PageInfo<Student>> myResult = new MyResult<>(StateCode.FIND_SUCCESS, "查询成功");
        PageInfo<Student> studentPageInfo = studentService.listCanceledAccount(pageHelp.getCurrentPage(), pageHelp.getPerPageSize());

        if (studentPageInfo != null) {
            myResult.setData(studentPageInfo);
            return myResult;
        }else {
            myResult.setCode(StateCode.FIND_FAILURE);
            myResult.setMessage("查询失败");
        }

        return myResult;
    }

    /**
     * 后台注销某个账号
     * @param id 主键Id
     * @return MyResult<Integer>
     */
    @PutMapping("/cancelAccount/{id}")
    public MyResult<Integer> cancelAccount(@PathVariable String id){
        MyResult<Integer> myResult = new MyResult<>(StateCode.UPDATE_SUCCESS, "注销成功");

        int result = studentService.cancelAccount(id);
        if (result > 0){
            return myResult;
        }else {
            myResult.setCode(StateCode.UPDATE_FAILURE);
            myResult.setMessage("注销失败");
        }
        return myResult;
    }

    /**
     * 恢复一个注销的账号
     * @param id 主键Id
     * @return MyResult<Integer>
     */
    @PutMapping("/recoverAccount/{id}")
    public MyResult<Integer> recoverAccount(@PathVariable String id){
        MyResult<Integer> myResult = new MyResult<>(StateCode.UPDATE_SUCCESS, "恢复成功");

        int result = studentService.releaseAccount(id);
        if (result > 0){
            return myResult;
        }else {
            myResult.setCode(StateCode.UPDATE_FAILURE);
            myResult.setMessage("恢复失败");
        }
        return myResult;
    }
}
